/**
* @program: src
*
* @description:数据库微服务
*
* @author: Mr.chen
*
* @create: 2020-12-18 15:55
**/
package main
import (
	"ydd-im/config"
	"ydd-im/service/api/define"
	"ydd-im/service/api/tools/util"
	"ydd-im/service/dbproxy/models"
	"ydd-im/service/dbproxy/util/etcd"
	"ydd-im/util/logger"
	"fmt"
	//"go.uber.org/zap"
	"log"
	"os"
	"os/signal"
	"syscall"
)
func main() {
	// init logger
	if err := logger.InitDbLogger(config.GetDbZapLog()); err != nil {
		log.Fatalf("init dbproxy logger failed, err:%v\n", err)
		return
	}
	//zap.L().Debug("this is hello func", zap.Int("age", 1))
	port :=getPort()
	etcdHost := config.GetDbProxyConfig().DbproxyEtcd.Host
	if len(etcdHost) > 0 {
		define.EtcdEndpoints = make([]string, 0)
		define.EtcdEndpoints = append(define.EtcdEndpoints, etcdHost)
	}
	//记录本机内网IP地址
	define.LocalHost = util.GetIntranetIp()
	models.InitDb()
	etcd.InitRPCServer(port)
	//将服务器地址、端口注册到etcd中
	etcd.RegisterServer()
	fmt.Println("|-----------------------------------|")
	fmt.Println("|          ydd-im              |")
	fmt.Println("|          author Mr.chen           |")
	fmt.Println("|-----------------------------------|")
	fmt.Println("| Go Dbproxy Server Start Successful|")
	fmt.Println("|    Port" + port + "     Pid:" + fmt.Sprintf("%d", os.Getpid()) + "        |")
	fmt.Println("|-----------------------------------|")
	fmt.Println("")
	// 注册数据库
	fmt.Println(define.LocalHost,define.RPCPort)
	signalChan := make(chan os.Signal)
	signal.Notify(signalChan, os.Interrupt,syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
	sig := <-signalChan
	log.Println("Get Signal:", sig)
	log.Println("Shutdown Server ...")
	log.Println("Server exiting")
}

// 自定义端口
func getPort() string {
	port := config.GetDbProxyConfig().DbproxyBase.Port
	args := os.Args //获取用户输入的所有参数
	if len(args) >= 2 && len(args[1]) != 0 {
		port = args[1]
	}
	define.Port = port
	return port
}






